#include "pch.h"
#include "GRiBounds3Box.h"
#include <array>
using namespace GMath;

using namespace DirectX;
void GRiBounds3Box::InitFromPoints(const GMath::Vector3& vi, const GMath::Vector3& vx)
{
	BoundingBox::CreateFromPoints(mBox, XMVectorSet(vi[0], vi[1], vi[2], 0), XMVectorSet(vx[0], vx[1], vx[2], 0));
}

GRiBounds3Box GRiBounds3Box::Transform(const GMath::Matrix& world) const
{
	GRiBounds3Box ans;
	mBox.Transform(ans.mBox, XMLoadFloat4x4(&world.mData));
	return ans;
}

GMath::Vector3 GRiBounds3Box::Center() const
{
	return mBox.Center;
}

GMath::Vector3 GRiBounds3Box::Extents() const
{
	return mBox.Extents;
}

GRiBounds3Box GRiBounds3Box::Union(const GRiBounds3Box& box) 
{
	BoundingBox::CreateMerged(mBox, mBox, box.mBox);
	return *this;
}

GRiBounds3Box GRiBounds3Box::Union(const GMath::Vector3& v) 
{
	GRiBounds3Box temp;
	temp.InitFromPoints(mBox.Center, v);
	return Union(temp);
}

int GRiBounds3Box::MaxExtent() const
{
	int i = 2;
	auto extents = Extents();
	if (extents[0] >= extents[1] && extents[0] >= extents[2])
		i = 0;
	else if (extents[1] >= extents[0] && extents[1] >= extents[2])
		i = 1;
	return i;
}

bool GRiBounds3Box::Intersect(const GMath::Vector3& orgin, const GMath::Vector3& dir)const
{
	
	float d = 0;
	return  mBox.Intersects(XMVectorSet(orgin[0], orgin[1], orgin[2], 0), XMVectorSet(dir[0], dir[1], dir[2], 0), d);
	
}
